/**
 * 功能描述 列表
 */

define(function (require, exports, module){
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var store =require("store");
	var planfile = require("../constant/planfile");
	
	var instance = {
		initComponent:function (params,widget){
			
			var cloneObj = function(obj){
			    var str, newobj = obj.constructor === Array ? [] : {};
			    if(typeof obj !== 'object'){
			        return;
			    } else if(window.JSON){
			        str = JSON.stringify(obj), //系列化对象
			        newobj = JSON.parse(str); //还原
			    } else {
			        for(var i in obj){
			            newobj[i] = typeof obj[i] === 'object' ? 
			            cloneObj(obj[i]) : obj[i]; 
			        }
			    }
			    return newobj;
			};
			
			var grid = new Grid({
				parentNode:".J-grid",
				url:"api/plan/query",
				autoRender:false,
				params:function(){
					var year = widget.get("subnav").getValue("select");
					var obj ={};
					if(year != ""){
						obj = {
								"year":moment(year).startOf("year").valueOf(),
								"yearEnd":moment(year).endOf("year").valueOf(),
								"orderString":"editorDate:DESC",
								fetchProperties:planfile.fetchProperties
							}
					}else{
						obj = {
								"orderString":"editorDate:DESC",
								fetchProperties:planfile.fetchProperties
							}
					}
					return obj;
				},
				model:{
					columns:[{
						name:"year",
						label:"年度",
						format:"date",
						formatparams:{
							mode:"YYYY"
						},
					},{
						name:"verNumber",
						label:"版本号",
						format:"detail",
						formatparams:{
							key:"show",
							handler:function(index,data,rowEle){
								
								
								var form = widget.get("form");
								form.reset();
								form.setData(data);
	            				form.setDisabled(true);
//
	            				
	            				var opt = widget.get("optiongrid");
	            				opt.setData(data.planItem);
	            				opt.setDisabled(true);
	            			
	            	            
	            				
	            				widget.set("rowData",data);
	            				
	            				widget.hide([".J-grid"]).show([".J-form",".J-optiongrid"]);
								widget.get("subnav").hide(["add","select"]).show(["return","printpreview","print"]);
								$(".J-grid-columns-operate").addClass("hidden");
							}
						}
					},{
						name:"planTitle",
						label:"计划名称"
					},{
						name:"editor",
						label:"编制人"
					},{
						name:"editorDate",
						label:"编制日期",
						format:"date",
						formatparams:{
							mode:"YYYY-MM-DD"
						}
					},{
						name:"status.value",
						label:"状态"
					},{
						name:"publisher",
						label:"发布人"
					},{
						name:"publisherDate",
						label:"发布日期",
						format:"date",
						formatparams:{
							mode:"YYYY-MM-DD"
						}
					},{
						name:"show",
						label:"操作",
						format:"button",
						formatparams:[{
							id:"edit",
							icon:"icon-edit",
							show:function(value,row){
								if(row.status.key == "UnRelease"){
									return true;
								}else{
									return false;
								}
							},
							handler:function(index,data,rowEle){
								var optiongrid = widget.get("optiongrid");
								optiongrid.setData([]);
								//修复数据
								var newplanitem = data.planItem;
								for(var i in newplanitem){
									
									if(newplanitem[i].limitmax){
										newplanitem[i].limitmax={
												key:"true",
												value:"限制"
											};
									}else{
										newplanitem[i].limitmax={
												key:"false",
												value:"不限制"
											};
									}
									
									if(newplanitem[i].limitmin){
										newplanitem[i].limitmin={
												key:"true",
												value:"限制"
											};
									}else{
										newplanitem[i].limitmin={
												key:"false",
												value:"不限制"
											};
									}
									
								}
								data.planItem = newplanitem;
								
								widget.get("form").reset();
								widget.hide([".J-grid"]).show([".J-form",".J-optiongrid"]);
								widget.get("subnav").hide(["add","select"]).show(["return","save"]);
								
								optiongrid.setData(cloneObj(data.planItem));
								
								var form = widget.get("form");
								form.setData(data);
								form.setValue("editorDate",moment());
								form.setValue("status",data.status.key);
								form.setValue("organization",data.organization.pkOrganization);
	            				form.setDisabled("editorDate",true);
	            				form.setDisabled("editor",true);
							}
						},{
							id:"torelease",
							text:"发布",
							show:function(value,row){
								if(row.status.key == "UnRelease"){
									return true;
								}else{
									return false;
								}
							},
							handler:function(index,data,rowEle){
	    						Dialog.confirm({
									title:"发布提示",
									defaultButton:false,
									content:"要发布的计划为: "+moment(data.year).format("YYYY")+" 年度下, "+data.verNumber+" 版本, "+data.planTitle+" 计划\n您确定要发布么?",
									buttons:[{
										id:"release",
					 					text:"确定",
					 					handler:function(){
					 						aw.ajax({
					 							url:"api/plan/release",
					 							data:{
													pkPlan : data.pkPlan
												},
					 							dataType:"json",
					 							success:function(data){
					 								widget.get("grid").refresh();
					 								Dialog.close();
					 							}
					 						});
					 					}
									},{
										id:"unrelease",
					 					text:"取消",
					 					handler:function(){
					 						Dialog.close();
					 					}
									}]
								});
							}
						},{
							id:"copyadd",
							type:"button",
							text:"复制新增",
							show:function(value,row){
								if(row.status.key == "Release"){
									return true;
								}else{
									return false;
								}
							},
							handler:function(index,data,rowEle){
								//将数据变为没有pk的新数据,用于新的存储,并做相应处理
								var newplan = cloneObj(data);
								newplan.pkPlan = "";//设置Plan的pk为null
								newplan.status = "UnRelease";
								delete newplan.organization;
								
								//数据填写form
								var form = widget.get("form");
	    						form.reset();
	    						form.setData(newplan);
								form.setValue("editorDate",moment());
	            				form.setDisabled("editorDate",true);
	            				form.setDisabled("editor",true);
	            				
	            				for(var i=0;i<newplan.planItem.length;i++){
	            					newplan.planItem[i].pkPlanItem = "";
	            					newplan.planItem[i].version = 0;
	            				}
	            				
	    						widget.get("optiongrid").setData(newplan.planItem);
	    						widget.get("optiongrid").setDisabled(false);
	    						
	    						widget.show([".J-form",".J-optiongrid"]).hide([".J-grid"]);
	    						widget.get("subnav").hide(["add","select"]).show(["return","save"]);
	    						//widget.get("optiongrid").update(index,newplan);
	    						//widget.get("optiongrid").refresh();
							}
						}]
					}]
				}
			});
			widget.set("grid",grid);
		}
	};
	module.exports = instance;
});
